{* $Id$ *}
{include_core file="admin/lib/header.html" title="加价购管理"}
<style>
  #js_upFile{
    position: absolute;
    top:0px;
    z-index: 20001;
    opacity: 0;
  }
  .js_editDiscount input{
    width:40px;
  }
</style>
<div class="page-header border-bottom clearfix">
  <h2 class="left">加价购管理<span>在这里对加价购进行管理</span></h2>
  <a href="javascript:void(0);" class="btn btn-success btn-sm right js_addPromotion"><i class="icon-add"></i>添加加价购商品</a>
</div>
<div class="search-level1 clearfix">
  <form action="goods/admin" method="get">
    <input type="hidden" name="is_advenced" value="0">
    <input type="hidden" name="status" value="{$status}">
    <input {if $is_advenced == 1}style="display:none;"{/if} type="text" class="left" name="search_filter[title]" value="{$search_filter.title}">
    <button {if $is_advenced == 1}style="display:none;"{/if} type="submit" class="btn btn-success btn-sm left">搜索</button>
  </form>
</div>
<!--start:列表-->
<table class="list-table">
  {*
  <caption>按状态：
    <a href='{AnUrl("promotion/admin?status=1")}' {if $status == 1} class="label label-primary" {else} class="label label-default" {/if}>正常</a>
    <a href='{AnUrl("promotion/admin?status=0")}' {if $status == 0} class="label label-primary" {else} class="label label-default" {/if}>删除</a>
  </caption>
  *}
  <thead><tr>
    {if $status == 1}
    <th style="width:20px;"><input type="checkbox" class="selectAll"></th>
    {/if}
    <th>ID</th>
    <th style="width:60px;">封面</th>
    <th style="min-width:230px;">商品信息</th>
    <th>价格</th>
    <th>促销价格</th>
    <th style="min-width:180px;">操作</th>
  </tr></thead>
  <tbody>
  {foreach $goods_list as $k=>$v}
  <tr id="dataList{$v.id}" data-price="{$v.price}" data-cover="{$image}" data-title="{$v.title}" {if $k%2 == 1} class="bg-gray"{/if} data-id="{$v.id}" data-relation_ids="{goods_helper::get_goods_relation_ids($v.id)}">
  {if $status == 1}
  <td><input type="checkbox" class="listSelect"></td>
  {/if}
  <td>
    {$v.id}
  </td>
  <td>
    <img width="70" src="{_image($v.cover, 'small')}">
  </td>
  <td>
    商品分类：{_uri('category', $v.category_id, 'title')}<br />
    商品标题：{$v.title}
  </td>
  <td>{$v.price}</td>
  <td class="js_salePrice" data-id="{$v.id}" data-sale_price="{$v.sale_price}">{$v.sale_price}</td>
  <td>
    <a href="javascript:void(0);" class="btn btn-info btn-xs js_editPromotion" data-type="edit"><i class="icon-edit icon-white"></i>编辑</a>
    <a href="{AnUrl("promotion/admin/delete?id={$v.id}")}" class="btn btn-danger btn-xs deleteOne"><i class="icon-trash icon-white"></i>删除</a>
  </td>
  </tr>
  {foreachelse}
  <tr><td class="txtleft" colspan="11">暂无数据</td></tr>
  {/foreach}
  </tbody>
  {if $status == 1}
  <tfoot>
  <tr>
    <a href="{AnUrl("goods/admin/delete")}" class="btn btn-danger btn-xs deleteAll">批量删除</a></td>
  </tr>
  </tfoot>
  {/if}
</table>
{include_core file="admin/lib/pager.html"}
<!--end:列表-->
{load_js file='zclip/jquery.zclip.min.js'}
{load_js file='zclip/ZeroClipboard.js'}
{load_js file='uploadify/jquery.uploadify.min.js'}
<script>
$(function(){
  // 高级搜索
  $('.js_advanced').click(function(e){
    e.preventDefault();
    var thisObj = $(this);
    var outBox  = thisObj.closest('form');
    if (thisObj.hasClass('js_isOpen')) {
      thisObj.removeClass('js_isOpen open');
      outBox.find('input').show();
      outBox.find('button').show();
      $('.js_advancedForm').hide();
    } else {
      thisObj.addClass('js_isOpen open');
      outBox.find('input').hide();
      outBox.find('button').hide();
      $('.js_advancedForm').show();
    }
  });

  // 添加
  $('.js_addPromotion').on('click', function(){
    addRelationGoods();
  });

  $('body').on('click', '.js_editPromotion', function(){
    var currType = $(this).data('type');
    var parentTr = $(this).closest('tr');
    if (currType == 'edit') {
      var salePrice = parseInt(parentTr.find('.js_salePrice').html());
      parentTr.find('.js_salePrice').html('<input type="text" value="'+salePrice+'">');
      $(this).html('<i class="icon-edit icon-white"></i>保存');
      $(this).data('type', 'save');
    } else {
      var salePrice = parentTr.find('.js_salePrice').find('input').val();
      var id        = parentTr.find('.js_salePrice').data('id');
      var url       = "{AnUrl('promotion/admin/eidt_goods')}";
      $.post(url, { id:id, sale_price:salePrice }, function(json){
        if (json.info == 'ok') {
          parentTr.find('.js_salePrice').html(json.sale_price);
          showMsg('修改成功');
        } else {
          showMsg(json.info);
        }
      }, 'json');
      $(this).html('<i class="icon-edit icon-white"></i>编辑');
      $(this).data('type', 'edit');
    }
  });
});

var getRelationList = function(goodsId)
{
  var url = "{AnUrl('goods/admin/get_relation_list')}"
  $.get(url, { goods_id:goodsId }, function(json){
    if (json.info == 'ok') {
      $('.js_realtionGoods').html(json.data);
      var height    = $(window).height();
      var divHeight = $('.js_discountDiv').height();
      var top = $(document).scrollTop()+(height - divHeight)/3;
      top = top < 0 ? 0: top;
      $('.js_discountDiv').css({ top:top+'px' });
    } else {
      $('.js_realtionGoods').html(json.info);
    }
  }, 'json');
}

var addRelationGoods = function() {
  Goods.option = {
    getDataUrl: "{AnUrl('promotion/admin/get_goods_list')}",
    sltGoods: 2,
    divTitle: '添加关联商品'
  };
  Goods.goodSelect(function (data) {
    var goodsIds = getGoodsIds().join(',');
    var priceArr = getGoodsSalePrice().join(',');

    $.ajax({
      type: "POST",
      url: "{AnUrl('promotion/admin/add_goods')}",
      data: { goods_ids:goodsIds, price_arr:priceArr },
      dataType: "json",
      async: false,
      cache: false,
      success: function (data) {
        if (data.info != 'ok') {
          showMsg(data.info);
          return false;
        } else {
          showMsg('添加成功');
          location.href = siteUrl+'/promotion/admin';
        }
      }
    })
  })
};

// 获取选中的商品ids
function getGoodsIds()
{
  var goodsIds = new Array();
  $('#js_sltRelationGoods').find('.js_goodsIds').each(function(){
    if ($(this).attr('checked') == 'checked') {
      var id = $(this).closest('.select-goods').data('id');
      if (!isNaN(id) && typeof id != 'undefind') {
        goodsIds.push(id);
      }
    }
  });

  return goodsIds;
}

// 获取商品折扣
function getGoodsSalePrice()
{
  var goodsIds = getGoodsIds();
  var idsLen = goodsIds.length;
  var discountArr = new Array();
  for (var i = 0; i < idsLen; i++) {
    var id = goodsIds[i];
    var discount = $('#js_sltRelationGoods').find('.goodsSale'+id).val();
    discountArr[i] = discount;
  }

  return discountArr;
}
</script>
{include_core file="admin/lib/footer.html"}